package cn.lwj.webService.client;

import cn.lwj.webService.entity.JsonResult;
import cn.lwj.webService.interceptor.SOAPHeaderInterceptor;
import cn.net.withub.webservice.client.rdjzbSjyw.SjywWebService;
import cn.net.withub.xfgl.xtgl.xtpt.SystemPlatformPort;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component(value="WsClient")
public class WsClient {
    private static Logger log  = Logger.getLogger(WsClient.class);

    public static String WEBSERVICE_URL = "http://localhost:8087/hlj/services/sjyw?wsdl";

    public static Object createFactoryBean(Class<?> myClass) throws java.lang.Exception {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(myClass);
        //factory.getOutInterceptors().add(new SOAPHeaderInterceptor("800","2400dataadmin","2400r"));
        factory.getOutInterceptors().add(new SOAPHeaderInterceptor("M00","2","withub000000"));
        //factory.getOutInterceptors().add(new SOAPHeaderInterceptor("700","750dataadmin","750g"));
        String wsdl_url = WEBSERVICE_URL;
        factory.setAddress(wsdl_url);
        return factory.create();
    }

    public static SystemPlatformPort getXtptWebServiceNew() {
        //创建JAX工厂类
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress("http://localhost:8087/hlj/services/sjyw?wsdl");
        //传入要调用的对象类
        factory.setServiceClass(SystemPlatformPort.class);
        //获取调用接口类的实现对象
        SystemPlatformPort wss = (SystemPlatformPort) factory.create();
        return wss;
    }

    public static SjywWebService getSjywWebService() {
        //创建JAX工厂类
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setAddress("http://localhost:8087/hlj/services/sjyw?wsdl");
        //传入要调用的对象类
        factory.setServiceClass(SjywWebService.class);
        //获取调用接口类的实现对象
        SjywWebService wss = (SjywWebService) factory.create();
        return wss;
    }


    public static List<Map<String,Object>> jsonToList(String jsonString) {
        List<Map<String,Object>> jsonList = new ArrayList<Map<String,Object>>();
        JSONArray jsonArray = JSONArray.fromObject(jsonString);
        if(jsonArray!=null&&jsonArray.size()>0){
            JSONObject jsonObject = null;
            Map<String,Object> jsonMap = null;
            for(Object object:jsonArray){
                jsonObject = JSONObject.fromObject(object);
                jsonMap = jsonToMap(jsonObject.toString());
                jsonList.add(jsonMap);
            }
        }
        return jsonList;
    }
    public static Map<String,Object> jsonToMap(String jsonString) {
        Map<String,Object> valueMap = new HashMap<String,Object>();
        JSONObject jsonObject = JSONObject.fromObject(jsonString);
        if(jsonObject!=null&&!jsonObject.toString().equals("null")){
            Iterator keyIter = jsonObject.keys();

            while (keyIter.hasNext()) {
                String key  = (String) keyIter.next();
                Object value = jsonObject.get(key);
                valueMap.put(key, value);
            }
        }

        return valueMap;
    }

    /*public static List<Map<String,Object>> test(String xml) throws Exception{
        List<Map<String,Object>> retList = new ArrayList<Map<String,Object>>();
        String departResource = "";
        try{
            SystemPlatformPort wss = getXtptWebServiceNew();//获取WEBSERVICE对象
            //获取部门

            SaveOrUpdateYqsq saveOrUpdateYqsq = new SaveOrUpdateYqsq();
            //saveOrUpdateYqsq.
            GetDepartmentListRequest getDepartmentListRequest = new GetDepartmentListRequest();
            getDepartmentListRequest.setCourtCode(fydm);
            GetDepartmentListResponse getDepartmentListResponse = wss.getDepartmentList(getDepartmentListRequest);
            if(getDepartmentListResponse!=null){
                departResource = getDepartmentListResponse.getBase64String();
                departResource = Base64.getFromBASE64(departResource);
                log.info("departResource==="+departResource);
                retList = jsonToList(departResource);
            }
        }catch (Exception e) {
            e.printStackTrace();
            log.info("获取系统平台部门异常");
        }
        return retList;
    }*/
    public static JsonResult test() throws Exception{
        String responseXml = "";
        try{
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<condition>\n" +
                    "    <cJzbId>2950012c0950-217a-48d1-8bb1-2e1652607133</cJzbId>\n" +
                    "    <nSqyqts>10</nSqyqts >\n" +
                    "    <cYqyy>疫情原因，无法调查</cYqyy>\n" +
                    "    <dtSqsj>2020-05-12</dtSqsj>\n" +
                    "    <cSqfybm>2950</cSqfybm>\n" +
                    "    <cSqfymc>重庆市高级人民法院</cSqfymc>\n" +
                    "    <cSqbmbm>069</cSqbmbm>\n" +
                    "    <cSqbmmc>立案庭</cSqbmmc>\n" +
                    "    <cSqrbm>222</cSqrbm>\n" +
                    "    <cSqrmc>张三</cSqrmc>\n" +
                    "    <nSpbz>0</nSpbz>\n" +
                    "    <nTbbz>1</nTbbz>\n" +
                    "    <cSpfybm>000</cSpfybm>\n" +
                    "    <cSpfymc>最高人民法院</cSpfymc>\n" +
                    "    <cSpbmbm>905</cSpbmbm>\n" +
                    "    <cSpbmmc>巡一庭</cSpbmmc>\n" +
                    "    <cSprbm>2</cSprbm>\n" +
                    "    <cSprmc>管理员1</cSprmc>\n" +
                    "    <cSpyjms></cSpyjms>\n" +
                    "    <nYqts>20</nYqts>\n" +
                    "    <dtSpsj>2020-06-02</dtSpsj>\n" +
                    "    <dtTimestamp>2020-06-03</dtTimestamp>\n" +
                    "</condition>\n";
            SjywWebService sjywWebService = (SjywWebService) createFactoryBean(SjywWebService.class);
            Client proxy = ClientProxy.getClient(sjywWebService);
            HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
            HTTPClientPolicy httpClientPolicy =  new  HTTPClientPolicy();
            httpClientPolicy.setConnectionTimeout(60000); //毫秒
            httpClientPolicy.setAllowChunking( false );
            httpClientPolicy.setReceiveTimeout(60000);
            conduit.setClient(httpClientPolicy);

            String xml1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<condition>\n" +
                    "    <cJzbId>2950012c0950-217a-48d1-8bb1-2e1652607133</cJzbId>\n" +
                    "</condition>";
            //responseXml = sjywWebService.saveOrUpdateYqsq(xml);
            //responseXml = sjywWebService.getYqsqjg(xml1);
            xml = "<condition>\n" +
                    "        <cJzbId>2950012c0950-217a-48d1-8bb1-2e1652607137</cJzbId>\n" +
                    "        <nSqyqts>10</nSqyqts >\n" +
                    "        <cYqyy>疫情原因，无法调查</cYqyy>\n" +
                    "        <dtSqsj>2020-05-12</dtSqsj>\n" +
                    "        <cSqfybm>2950</cSqfybm>\n" +
                    "        <cSqfymc>重庆市高级人民法院</cSqfymc>\n" +
                    "        <cSqbmbm>069</cSqbmbm>\n" +
                    "        <cSqbmmc>立案庭</cSqbmmc>\n" +
                    "        <cSqrbm>222</cSqrbm>\n" +
                    "        <cSqrmc>张三</cSqrmc>\n" +
                    "        <dtTimestamp>2020-06-02</dtTimestamp>\n" +
                    "    </condition>";
            //responseXml = sjywWebService.saveYqsq(xml);
            xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "    <condition>\n" +
                    "        <cJzbId>2950012c0950-217a-48d1-8bb1-2e1652607136</cJzbId>\n" +
                    "        <nSqyqts>10</nSqyqts >\n" +
                    "        <cYqyy>测试:疫情原因，无法调查</cYqyy>\n" +
                    "        <dtSqsj>2020-05-12</dtSqsj>\n" +
                    "        <cSqfybm>2950</cSqfybm>\n" +
                    "        <cSqfymc>重庆市高级人民法院</cSqfymc>\n" +
                    "        <cSqbmbm>069</cSqbmbm>\n" +
                    "        <cSqbmmc>立案庭</cSqbmmc>\n" +
                    "        <cSqrbm>222</cSqrbm>\n" +
                    "        <cSqrmc>张三</cSqrmc>\n" +
                    "        <nSpbz>3</nSpbz>\n" +
                    "        <cSpfybm>000</cSpfybm>\n" +
                    "        <cSpfymc>最高人民法院</cSpfymc>\n" +
                    "        <cSpbmbm>905</cSpbmbm>\n" +
                    "        <cSpbmmc>巡一庭</cSpbmmc>\n" +
                    "        <cSprbm>2</cSprbm>\n" +
                    "        <cSprmc>管理员</cSprmc>\n" +
                    "        <cSpyjms>同意</cSpyjms>\n" +
                    "        <nYqts>20</nYqts>\n" +
                    "        <dtSpsj>2020-06-02</dtSpsj>\n" +
                    "        <dtTimestamp>2020-06-02</dtTimestamp>\n" +
                    "    </condition>" ;
            //responseXml = sjywWebService.updateYqsq(xml);

            xml = "<condition>\n" +
                    "  <cJzbId>2950012c0950-217a-48d1-8bb1-2e1652607134</cJzbId>\n" +
                    "</condition>";
            //responseXml = sjywWebService.getYqsqjg(xml);
            //responseXml = sjywWebService.getDspYqsqList(xml);

            xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "    <condition>\n" +
                    "        <cYqxkId>4</cYqxkId>\n" +
                    "        <cJzbIds>1111111111111111111111111111111,222222222222,33333333333333333333</cJzbIds>\n" +
                    "        <cYqyy>疫情原因，无法调查</cYqyy>\n" +
                    "        <dtSqsj>2020-05-12</dtSqsj>\n" +
                    "        <cSqfybm>2950</cSqfybm>\n" +
                    "        <cSqfymc>重庆市高级人民法院</cSqfymc>\n" +
                    "        <cSqbmbm>069</cSqbmbm>\n" +
                    "        <cSqbmmc>立案庭</cSqbmmc>\n" +
                    "        <cSqrbm>222</cSqrbm>\n" +
                    "        <cSqrmc>张三</cSqrmc>\n" +
                    "        <dtTimestamp>2020-06-02</dtTimestamp>\n" +
                    "    </condition>";
            responseXml = sjywWebService.saveYqxk(xml);
            xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "    <condition>\n" +
                    "        <cYqxkId>1</cYqxkId>\n" +
                    "        <cYqyy>疫情原因，无法调查</cYqyy>\n" +
                    "        <dtSqsj>2020-05-12</dtSqsj>\n" +
                    "        <cSqfybm>2950</cSqfybm>\n" +
                    "        <cSqfymc>重庆市高级人民法院</cSqfymc>\n" +
                    "        <cSqbmbm>069</cSqbmbm>\n" +
                    "        <cSqbmmc>立案庭</cSqbmmc>\n" +
                    "        <cSqrbm>222</cSqrbm>\n" +
                    "        <cSqrmc>张三</cSqrmc>\n" +
                    "        <nSpbz>1</nSpbz>\n" +
                    "        <cSpfybm>000</cSpfybm>\n" +
                    "        <cSpfymc>最高人民法院</cSpfymc>\n" +
                    "        <cSpbmbm>905</cSpbmbm>\n" +
                    "        <cSpbmmc>巡一庭</cSpbmmc>\n" +
                    "        <cSprbm>2</cSprbm>\n" +
                    "        <cSprmc>管理员</cSprmc>\n" +
                    "        <cSpyjms>同意</cSpyjms>\n" +
                    "        <nYqts>20</nYqts>\n" +
                    "        <dtSpsj>2020-06-02</dtSpsj>\n" +
                    "        <dtTimestamp>2020-06-02</dtTimestamp>\n" +
                    "    </condition>";
            log.info(responseXml);


        }catch (Exception e) {
            e.printStackTrace();
            log.info("获取Ws异常");
        }
        return JsonResult.responseData(200,"",responseXml);
    }


}
